package com.vader.boot.beanFactory;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultSingletonBeanRegistry;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.io.Resource;

import java.lang.reflect.Field;

/**
 * @author Vader
 * @date 2022/3/23
 **/
@SpringBootApplication
@Slf4j
public class BeanFactoryApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext run = SpringApplication.run(BeanFactoryApplication.class, args);
        try {
            //singletonObjects
            Field singletonObjects = DefaultSingletonBeanRegistry.class.getDeclaredField("singletonObjects");
            ConfigurableListableBeanFactory beanFactory = run.getBeanFactory();
            singletonObjects.setAccessible(true);
            Object o = singletonObjects.get(beanFactory);


            //ApplicationContext
            Resource[] resources = run.getResources("classpath*:META-INF/spring.factories");
            for (Resource resource : resources) {
                log.info("resource:{}",resource);
            }

            ConfigurableEnvironment environment = run.getEnvironment();
            String java_home = environment.getProperty("java_home");
            String property = environment.getProperty("server.port");
            log.info("getEnvironment_info:{}_{}",java_home,property);

            Component2 component1 = (Component2) run.getBean("component2");
            component1.uploadFile();
        } catch (Exception e) {
            log.error("getDeclaredField_error:{}", e.getMessage());
        }
    }
}
